package frontend.components.other;

import backend.model.admin.Employee;
import backend.model.admin.Team;
import backend.model.other.User;
import shared.other.UserRolesDefinition;
import backend.service.admin.TeamService;
import backend.service.other.UserService;
import frontend.core.TimeTrackingWebApplication;
import frontend.core.TimeTrackingWebSession;
import frontend.model.base.UIModel;
import frontend.components.base.ComponentFactory;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.panel.Panel;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class TeamSelectionPanel<UIM extends UIModel> extends Panel {

    private User loggedInUser;

    public TeamSelectionPanel(String id, UIM model, UserInputCallableAction<Team> onSelectionAction, String labelResourceKey, boolean isNullValid) {
        super(id);

        String loggedInUsername = (String) TimeTrackingWebSession.get().getAttribute(TimeTrackingWebSession.LOGIN_USERNAME_ATTR);
        loggedInUser = TimeTrackingWebApplication.getService(UserService.class).findUserByUsername(loggedInUsername);

        boolean isForeman = UserRolesDefinition.UserRole.FOREMAN.equals(loggedInUser.getRole());
        boolean isAdmin = UserRolesDefinition.UserRole.ADMIN.equals(loggedInUser.getRole());

        List<Team> availableTeams = new ArrayList<Team>();
        if (isForeman) {
            availableTeams.addAll(getAllActiveTeamsForForeman());
        } else if (isAdmin) {
            availableTeams.addAll(getAllActiveTeams());
        }
        removeDefaultTeam(availableTeams);
        WebMarkupContainer teamComponent = ComponentFactory.createDropDownChoiceComponent("team", labelResourceKey,
                model, availableTeams, new DBModelChoiceRenderer<Team>(), isNullValid, onSelectionAction);
        teamComponent.setVisible(isAdmin || isForeman);

        add(teamComponent);
    }

    private void removeDefaultTeam(List<Team> availableTeams) {
        Team defaultTeam = null;
        for (Team team : availableTeams) {
            if (team.getId() == 1) {
                defaultTeam = team;
            }
        }
        availableTeams.remove(defaultTeam);
    }

    private List<Team> getAllActiveTeams() {
        List<Team> availableTeams = new ArrayList<Team>();
        availableTeams.addAll(TimeTrackingWebApplication.getService(TeamService.class).findAllActiveTeams());
        Collections.sort(availableTeams);
        return availableTeams;
    }

    private List<Team> getAllActiveTeamsForForeman() {
        List<Team> availableTeams = new ArrayList<Team>();
        Employee employee = loggedInUser.getEmployee();
        availableTeams.addAll(TimeTrackingWebApplication.getService(TeamService.class).findAllActiveTeamsOfForeman(employee.getForeman().getId()));
        Collections.sort(availableTeams);
        return availableTeams;
    }
}
